package com.ce.pms.system.custservice.service.impl;

import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.ICustEventNatureDao;
import com.ce.pms.system.custservice.model.CustEventNatureDO;
import com.ce.pms.system.custservice.vo.CustEventNatureVO;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 描述：测试“事件性质”service
 * @author 王浩羽
 *
 */
public class CustEventNatureServiceImplT extends MyJMockBaseTestCase
{
	private ICustEventNatureDao custEventNatureDao;
	private CustEventNatureServiceImpl custEventNatureServiceImpl;
	private CustEventNatureVO natureVO ;
	private CustEventNatureDO natureDO ;

	//常量数据
	private final Long CURRENT_ID = 2008L;
	private final String CURRENT_NAME = "oldName";
	private final String CURRENT_REMARK = "oldRemark";
	private final short CURRENT_SEQUENCE = (short)1;
	
	private final Long NEW_ID = 2009L;
	private final String NEW_NAME = "newName";
	private final String NEW_REMARK = "newRemark";
	private final short NEW_SEQUENCE = (short)2;
	@Before
	public void setUp() throws Exception
	{
		super.setUp();
		// set up
		custEventNatureDao = context.mock(ICustEventNatureDao.class);
		custEventNatureServiceImpl = new CustEventNatureServiceImpl();
		custEventNatureServiceImpl.setCustEventNatureDao(custEventNatureDao);
		custEventNatureServiceImpl.setSysrefcodeDao(sysrefcodeDao);
		natureVO = new CustEventNatureVO();
		natureDO = new CustEventNatureDO();
		
		natureDO.setKeyword(CURRENT_ID);
		natureDO.setName(CURRENT_NAME);
		natureDO.setRemark(CURRENT_REMARK);
		natureDO.setSequence(CURRENT_SEQUENCE);
		natureDO.setSysInfo(sysInfo);
		
		//VO暂不设置ID，如果VO有ID，使用NEW_ID
		natureVO.setName(NEW_NAME);
		natureVO.setRemark(NEW_REMARK);
		natureVO.setSequence(NEW_SEQUENCE);
	}


	@After
	public void tearDown() throws Exception
	{

	}
	
	@Test
	public void testAddCustServEventNatureFail()
	{
		natureVO.setName(natureDO.getName());//不一定要这么做，这只是说明已经存在同名称的记录
		natureVO.setCreator(SYSTEM_INFO_CREATER);
		// expectations
		context.checking(new Expectations() {{
			//当名称存在时，将抛出异常，保存操作不会执行
			oneOf(custEventNatureDao).findUniqueByName(natureVO.getName()); will(returnValue(natureDO));
			never(sysrefcodeDao).getDate();will(returnValue(SYSTEM_INFO_DATE_CREATED));
			never(custEventNatureDao).saveObject(with(any(CustEventNatureDO.class))); 
	    }});
		// execute
		try
		{
			boolean returnValue = custEventNatureServiceImpl.addCustEventNature(natureVO);
			fail("it should throw exception!");
		}
		catch(Exception e)
		{
			assertTrue(e instanceof BusinessException);
		}
		// verify
		context.assertIsSatisfied();
	}
	
	@Test
	public void testAddCustServEventNatureSuccess()
	{
		natureVO.setCreator(SYSTEM_INFO_CREATER);
		// expectations
		context.checking(new Expectations() {{
			//当名称还没有存在时，保存操作执行
			oneOf(custEventNatureDao).findUniqueByName(natureVO.getName()); will(returnValue(null));
			oneOf(sysrefcodeDao).getDate();will(returnValue(SYSTEM_INFO_DATE_CREATED));
			oneOf(custEventNatureDao).saveObject(with(any(CustEventNatureDO.class))); will(returnValue(natureDO));
	    }});
		// execute
		boolean returnValue = custEventNatureServiceImpl.addCustEventNature(natureVO);
		// verify
		context.assertIsSatisfied();
		assertTrue(returnValue);
	}

	@Test
	public void testEditCustServEventNatureFail()
	{
		natureVO.setKeyword(NEW_ID);	
		natureVO.setModifier(SYSTEM_INFO_NEW_MODIFIER);
		// expectations
		context.checking(new Expectations() {{
			//当数据库没有存在待修改记录时,不在进行修改操作
			oneOf(custEventNatureDao).findById(natureVO.getKeyword()); will(returnValue(null));
			never(sysrefcodeDao).getDate();will(returnValue(SYSTEM_INFO_DATE_CREATED));
			never(custEventNatureDao).updateObject(with(any(CustEventNatureDO.class)));
	    }});
		// execute
		boolean returnValue = custEventNatureServiceImpl.editCustEventNature(natureVO);
		// verify
		context.assertIsSatisfied();
		assertFalse(returnValue);
	}
	
	@Test
	public void testEditCustServEventNatureSuccess()
	{
		natureVO.setKeyword(CURRENT_ID);
		natureVO.setModifier(SYSTEM_INFO_NEW_MODIFIER);
		final CustEventNatureDO natureDO_toEdit = new CustEventNatureDO();
		copyData(natureDO, natureDO_toEdit);
		natureDO_toEdit.setKeyword(natureVO.getKeyword());
		
		// expectations
		context.checking(new Expectations() {{
			//当数据库存在待修改记录时,
			oneOf(custEventNatureDao).findById(natureVO.getKeyword()); will(returnValue(natureDO_toEdit));
			oneOf(sysrefcodeDao).getDate();will(returnValue(SYSTEM_INFO_DATE_CREATED));
			//进行修改操作
			oneOf(custEventNatureDao).updateObject(natureDO_toEdit); will(returnValue(natureDO_toEdit));
	    }});
		// execute
		boolean returnValue = custEventNatureServiceImpl.editCustEventNature(natureVO);
		// verify
		context.assertIsSatisfied();
		assertTrue(returnValue);
		//数据已经被更改
		assertEquals(natureVO.getRemark(), natureDO_toEdit.getRemark());
		assertTrue(natureVO.getSequence()==natureDO_toEdit.getSequence());
	}
	
	@Test
	public void testDeleteCustServEventNatureByIdFail()
	{
		// expectations
		context.checking(new Expectations() {{
			//抛出异常
			oneOf(custEventNatureDao).deleteObjectById(CURRENT_ID);will(throwException(new RuntimeException("被引用")));
	    }});
		// execute
		try
		{
			boolean returnValue = custEventNatureServiceImpl.deleteCustEventNatureById(CURRENT_ID);
			fail("it should throw exception!");
		}
		catch(Exception e)
		{
			assertTrue(e instanceof BusinessException);
		}
		// verify
		context.assertIsSatisfied();
	}
	
	
	@Test
	public void testDeleteCustServEventNatureByIdSuccess()
	{
		// expectations
		context.checking(new Expectations() {{
			//不抛异常
			oneOf(custEventNatureDao).deleteObjectById(CURRENT_ID);
	    }});
		// execute
		try
		{	
			boolean returnValue = custEventNatureServiceImpl.deleteCustEventNatureById(CURRENT_ID);
			assertTrue(returnValue);
		}
		catch(Exception e)
		{
			fail("it should not throw exception! "+e.getMessage());
		}
		// verify
		context.assertIsSatisfied();
	}
	
	@Test
	public void testQueryCustServEventNatureListByPage()
	{
		final SearchVO searchVO = new SearchVO();
		Integer currentPage = 2; 
		Integer pageSize = 5;
		searchVO.setPageNo(currentPage);
		searchVO.setPageSize(pageSize);
		
		final Page page = new Page();
		//这个测试比较简单，因为这里没有多少业务逻辑，主要的逻辑已经下移到dao层了，可能在dao层需要做的工作会多些
		// expectations
		context.checking(new Expectations() {{
			oneOf(custEventNatureDao).queryByPage(searchVO); will(returnValue(page));
	    }});
		Page returnValue = custEventNatureServiceImpl.queryCustEventNatureListByPage(searchVO);
		context.assertIsSatisfied();
		assertNotNull(returnValue);
	}

}
